package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.domain.req.MachiningReq;
import com.ruibang.glass.produce.domain.resp.PositionSurveyGraphResp;
import com.ruibang.glass.produce.entity.DbMeasure;
import com.ruibang.glass.produce.entity.DbPositionSurvey;
import com.ruibang.glass.produce.entity.DbProcessWaterFlowRate;
import com.ruibang.glass.produce.mapper.DbPositionSurveyMapper;
import com.ruibang.glass.produce.service.DbPositionSurveyService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.ZoneOffset;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-01-23  10:43
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@Slf4j
public class DbPositionSurveyServiceImpl extends ServiceImpl<DbPositionSurveyMapper, DbPositionSurvey> implements DbPositionSurveyService {

    @Override
    public IPage<DbPositionSurvey> pageQuery(MachiningReq machiningReq) {
        long end = machiningReq.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long start = machiningReq.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        final IPage<DbPositionSurvey> page = new Page<>(machiningReq.getPageNo(), machiningReq.getPageSize());
        return baseMapper.pageQuery(page, start, end, machiningReq.getGlassId());
    }

    @Override
    public void export(MachiningReq machiningReq, HttpServletResponse response) {
        machiningReq.setPageNo(-1);
        machiningReq.setPageSize(-1);
        IPage<DbPositionSurvey> page = this.pageQuery(machiningReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<DbPositionSurvey> dbPositionSurveyList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), DbPositionSurvey.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("短边研磨短边研磨定位测量数据").doWrite(dbPositionSurveyList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public PositionSurveyGraphResp positionSurveyGraph(MachiningReq machiningReq) {
        long end = machiningReq.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long start = machiningReq.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        List<DbPositionSurvey> dbPositionSurveyList = baseMapper.positionSurveyGraph(start, end);

        if (CollectionUtils.isNotEmpty(dbPositionSurveyList)) {
            PositionSurveyGraphResp positionSurveyGraphResp = new PositionSurveyGraphResp();
            //采集时间
            List<String> collectTime = dbPositionSurveyList.stream().map(DbPositionSurvey::getDateTime).collect(Collectors.toList());
            positionSurveyGraphResp.setCollectTime(collectTime);
            //前端
            List<String> qd = dbPositionSurveyList.stream().map(DbPositionSurvey::getDwblclQd).collect(Collectors.toList());
            positionSurveyGraphResp.setQd(qd);
            //左上游
            List<String> zsy = dbPositionSurveyList.stream().map(DbPositionSurvey::getDwblclZs).collect(Collectors.toList());
            positionSurveyGraphResp.setZsy(zsy);
            //左下右
            List<String> zxy = dbPositionSurveyList.stream().map(DbPositionSurvey::getDwblclZx).collect(Collectors.toList());
            positionSurveyGraphResp.setZxy(zxy);
            //右上游
            List<String> ysy = dbPositionSurveyList.stream().map(DbPositionSurvey::getDwblclYs).collect(Collectors.toList());
            positionSurveyGraphResp.setYsy(ysy);
            //右下游
            List<String> yxy = dbPositionSurveyList.stream().map(DbPositionSurvey::getDwblclYx).collect(Collectors.toList());
            positionSurveyGraphResp.setYxy(yxy);
            //后端
            List<String> hd = dbPositionSurveyList.stream().map(DbPositionSurvey::getDwblclHd).collect(Collectors.toList());
            positionSurveyGraphResp.setHd(hd);

            return positionSurveyGraphResp;
        }

        return null;
    }
}
